/**
 * account table.
 */
var accountTable = {};

var prefix = "o:a:";
var skey = "o:a:s";
var prefix_t = "o:a:t:";
var prefix_p = "o:a:p:";

function gettime() {
	"use strict";
	var d = new Date();
	return d.toUTCString();
}

function defaultCallback(err) {
	if (err) {
		console.error("accountTable redis error:" + err);
	}
}

accountTable.add = function(context, acc, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = prefix + acc.id;

	acc.regtime = gettime();

	context.beginMulti();
	redis.hmset(key, acc, callback);
	redis.sadd(skey, acc.id, defaultCallback);
	context.endMulti(defaultCallback);
};

accountTable.loadFields = function(context, id, fields, callback) {
	"use strict";
	var redis = context.redis;
	
	var key = prefix + id;
	var fields2 = [key];
	fields2 = fields2.concat(fields);
	redis.hmget(fields2, callback);
};

accountTable.setFields = function(context, id, fields, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = prefix + id;

	redis.hmset(key, fields, callback);
};

accountTable.del = function(context, id, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = prefix + id;
	context.beginMulti();
	redis.srem(skey, id, defaultCallback);
	redis.del(key, callback);
	redis.del(prefix_t + id, defaultCallback);
	context.endMulti(defaultCallback);
};

accountTable.scan = function(context, cursor, count, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	redis.sscan([skey, cursor, "COUNT", count], callback);
};

accountTable.count = function(context, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	redis.scard(skey, callback);
};

accountTable.add_t = function(context, accountId, taskId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var field_name = taskId;
	var key = prefix_t + accountId;
	redis.sadd(key, field_name, callback);
};

accountTable.del_t = function(context, accountId, taskId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var field_name = taskId;
	var key = prefix_t + accountId;
	redis.srem(key, field_name, callback);
};

accountTable.getAll_t = function(context, accountId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	var key = prefix_t + accountId;
	redis.smembers(key, callback);
};

accountTable.add_p = function(context, accountId, planId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var field_name = planId;
	var key = prefix_p + accountId;
	redis.sadd(key, field_name, callback);
};

accountTable.del_p = function(context, accountId, planId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var field_name = planId;
	var key = prefix_p + accountId;
	redis.srem(key, field_name, callback);
};

accountTable.getAll_p = function(context, accountId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	var key = prefix_p + accountId;
	redis.smembers(key, callback);
};

module.exports = accountTable;
